package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author lfh
 * @version 1.0
 * @date 2021/5/13 10:28
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    // 分页
    // http://localhost:8080/supplier/list

    /**
     * 分页查询供应商
     * @param page  当前页数
     * @param rows  每页显示的记录数
     * @param supplierName  供应商名称
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> list(Integer page, Integer rows, String supplierName){
        return supplierService.list(page,rows,supplierName);
    }

    /**
     * 供应商添加或修改
     * @param supplier
     * @return
     */
    //http://localhost:8080/supplier/save?supplierId=1
    @PostMapping("/save")
    @ResponseBody
    public ServiceVO save(Supplier supplier){
        try {
            supplierService.save(supplier);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }catch (Exception e){
            return new ServiceVO(400, "保存失败");
        }

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    // http://localhost:8080/supplier/delete
    @PostMapping("/delete")
    @ResponseBody
    public ServiceVO delete(String ids){
        try {
            supplierService.delete(ids);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            return new ServiceVO(400, "删除失败");
        }
    }
}
